import { Component, OnInit, NgModule } from '@angular/core';
import { Router, ActivatedRoute } from '@angular/router';
import { ConmonService } from "../../service/common.service";
// declare var $: any;
import * as $ from 'jquery';
@Component({
  selector: 'app-project-total',
  templateUrl: './project-total.component.html',
  styleUrls: ['./project-total.component.scss']
})
@NgModule({
  providers: [
    ConmonService
  ]
})
export class ProjectTotalComponent implements OnInit {
  private tokenObj: any;
  private dataList: any;
  private flag: any;
  private pages: number = 1;
  private arrList: any;
  private totalPages: any;
  private totalData: any;
  private domainVal: string = "afcat.com.cn";
  private url: string = "http://crmdev.afcat.com.cn:8001/psc/CRMDEV/EMPLOYEE/CRM/s/WEBLIB_SI_D.CRM_SI_HTTP.FieldFormula.IScript_CRM_Service";
  constructor(private router: Router, private service: ConmonService, private routerIonfo: ActivatedRoute) {
    this.flag = this.routerIonfo.snapshot.params["flag"];
    var dateObj = {
      AccessPages: this.pages
    };
    this.arrList = [];
    this.dataList = [];
    // this.tokenObj = service.getToken(1, dateObj, 'ProjectWorkListInfo', false);
    this.tokenObj = service.token;
    if (this.flag === 'false') {
      this.totalData = service.projectScreeningData;
      this.totalPages = this.totalData.body.totalPages;
      this.arrList = this.totalData.body.ProjectWorkListInfo;
      this.dataList = this.dataList.concat(this.arrList);
      var arr = this.dataList;
    } else if (this.flag === 'true') {
      this.totalData = service.withBodyData(dateObj, 'ProjectWorkListInfo');
      this.totalPages = this.totalData.body.totalPages;
      this.arrList = this.totalData.body.ProjectWorkListInfo;
      this.dataList = this.dataList.concat(this.arrList);
      var arr = this.dataList;
    }
    //循环遍历数据，然后处理报工日期
    for (var i = 0; i < arr.length; i++) {
      var oldWorkDate = arr[i].WorkDate;
      var newWorkDate = oldWorkDate.replace('-', '');
      newWorkDate = newWorkDate.replace('-', '');
    }
    //按照报工日期排序进行展示
    for (var i = 0; i < arr.length - 1; i++) {
      for (var j = i + 1; j < arr.length; j++) {
        if (arr[i].WorkDate < arr[j].WorkDate) {
          var baba = arr[j];
          arr[j] = arr[i];
          arr[i] = baba;
        };
      };
    };
    console.log(this.tokenObj);
    console.log(this.dataList);
    // this.getMethod('myApplyWorkInfo');
  }
  public screeningMethod(): void {
    this.router.navigate(['/ProjectScreening']);
  }
  private page: Object = { num: 1, size: 10 };
  scrollEvent(event) {
    // console.log(event.target.scrollTop);
    let _this = this;
    let scrollDiv = document.getElementById('projectTatol');

    let oldScrollTop = scrollDiv.scrollTop;
    let scrollHeight = scrollDiv.scrollHeight;
    let clientHeight = scrollDiv.clientHeight;
    // console.log(oldScrollTop);
    // console.log(clientHeight);
    console.log(scrollHeight);
    if ((oldScrollTop + clientHeight >= scrollHeight) && (_this.pages < _this.totalPages)) {
      console.log(_this.pages, _this.totalPages);
      // _this.pages++;
      upCallback();
    }
    function upCallback() {
      // let _this = this;
      if (_this.pages >= _this.totalPages) {
        _this.pages = _this.totalPages;
      } else {
        _this.pages++;
      }
      console.log('上拉');
      // if (_this.pages <= _this.totalPages) {
      console.log(_this.pages);
      let dateObj = {
        AccessPages: _this.pages
      };
      // _this.tokenObj = _this.service.getToken(1, dateObj, 'backlogListInfo', true);
      _this.arrList = _this.service.withBodyData(dateObj, 'ProjectWorkListInfo').body.ProjectWorkListInfo;
      _this.dataList = _this.dataList.concat(_this.arrList);
      var arr = _this.dataList;
      console.log(arr);
      //按照报工日期排序进行展示
      for (var i = 0; i < arr.length - 1; i++) {
        for (var j = i + 1; j < arr.length; j++) {
          if (arr[i].WorkDate < arr[j].WorkDate) {
            var baba = arr[j];
            arr[j] = arr[i];
            arr[i] = baba;
          };
        };
      };
      // };
    };
  };
  // 项目报工上拉下拉结束
  ngOnInit() {
    let _this = this;
    if (this.flag === 'true') {
      if (document.getElementById('projectTatol')) {
        var mescroll = new MeScroll("projectTatol", {
          down: {
            callback: downCallback //下拉刷新的回调,别写成downCallback(),多了括号就自动执行方法了
          }
          // up: {
          //   callback: upCallback
          // }
        });
      };
    }
    //下拉刷新的回调
    function downCallback() {
      console.log('下拉');
      // _this.getWorkAuditData();
      //数组去重方法
      function unique5(array) {
        var r = [];
        for (var i = 0, l = array.length; i < l; i++) {
          for (var j = i + 1; j < l; j++)
            if (array[i].SubmitNo === array[j].SubmitNo) j = ++i;
          r.push(array[i]);
        }
        return r;
      };
      $.ajax({
        url: _this.service.url,
        async: false, // 同步请求 false，异步请求 true
        type: "POST", // 请求类型，POST、GET
        xhrFields: {
          withCredentials: true
        },
        crossDomain: true,
        dataType: 'json',
        // data: { crmData: "{'head': {'ServName':'ProjectWorkListInfo','SeqNo':'123123123123123123123123','employId':'000123'},'body':{'AccessPages':'1'}}" },
        data: { crmData: "{'head': {'ServName':'ProjectWorkListInfo','SeqNo':'123123123123123123123123','employId':"+"'"+_this.service.employId+"'"+"},'body':{'AccessPages':'1'}}" },
        success: function (data) {
          console.log(data);
          //联网成功的回调,隐藏下拉刷新的状态;
          mescroll.endSuccess(); //无参
          //设置数据
          // _this.dataList = [];
          let totalData = data;
          _this.totalPages = totalData.body.totalPages;
          _this.arrList = totalData.body.ProjectWorkListInfo;
          //数组合并
          _this.dataList = _this.dataList.concat(_this.arrList);
          _this.dataList = unique5(_this.dataList);
          console.log(_this.dataList);
          var arr = _this.dataList;
          //按照报工日期排序进行展示
          for (var i = 0; i < arr.length - 1; i++) {
            for (var j = i + 1; j < arr.length; j++) {
              if (arr[i].WorkDate < arr[j].WorkDate) {
                var baba = arr[j];
                arr[j] = arr[i];
                arr[i] = baba;
              };
            };
          };
        },
        error: function (data) {
          //联网失败的回调,隐藏下拉刷新的状态
          mescroll.endErr();
        }
      });
    };
  }

}
